/*
 * @(#)PagerTagTestBase.java $version 2010. 8. 11
 *
 * Copyright 2007-2010 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nhncorp.usf.web.taglibs.pager;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;

import org.junit.Before;
import org.junit.BeforeClass;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

import com.nhncorp.usf.core.config.PagerConfigInfo;
import com.nhncorp.usf.core.config.PagerConfigInfos;
import com.nhncorp.usf.core.result.template.directive.PagerInfo;

/**
 * Pager Taglibrary 테스트 지원을 위한 추상클클래스.
 *
 * @author Web Platform Development Team
 */
public abstract class PagerTagTestBase {

	protected MockPageContext pageContext;

	protected MockHttpServletRequest request;

	protected MockHttpServletResponse response;

	protected NavigatorTag navigatorTag;

	protected PagerInfo pagerInfo;

	protected String requestURI = "http://dev.naver.com/usf/page.nhn";

	protected int totalRows = 400;

	protected int currentPage = 3;

	protected String type = "default";

	@BeforeClass
	public static void setUp() {
		PagerConfigInfo info = new PagerConfigInfo();
		info.setPageSize(10);
		info.setIndexSize(10);
		info.setStepType("page");
		info.setFirst("{0}first{1}");
		info.setFirstOff("");
		info.setPrev("{0}{1}");
		info.setPrevOff("prev-off");
		info.setNext("{0}{1}");
		info.setNextOff("aaa");
		info.setLast("{0}last{1}");
		info.setLastOff("");
		info.setSeperator("");
		info.setIndex("{0}{1}{2}");
		info.setCurrent("{0}{1}{2}");
		PagerConfigInfos.put("default", info);

		info = new PagerConfigInfo();
		info.setType("javascript");
		info.setPageSize(10);
		info.setIndexSize(10);
		info.setStepType("page");
		info.setFirst("<a href=\"javascript:paging({2})\">first</a>");
		info.setFirstOff("");
		info.setPrev("<a href=\"javascript:paging({2})\">prev</a>");
		info.setPrevOff("");
		info.setNext("<a href=\"javascript:paging({2})\">next</a>");
		info.setNextOff("");
		info.setLast("<a href=\"javascript:paging({2})\">last</a>");
		info.setLastOff("");
		info.setSeperator("");
		info.setIndex("<a href=\"javascript:paging({1})\">{1}</a>");
		info.setCurrent("<a href=\"javascript:paging({1})\">{1}</a>]");
		PagerConfigInfos.put("javascript", info);
	}

	@Before
	public void prepareTag() throws Exception {
		request = new MockHttpServletRequest();
		response = new MockHttpServletResponse();
		
		request.setAttribute("totalRow", totalRows);
		pageContext = new MockPageContext(null, request, response);

		navigatorTag = new NavigatorTag();
		navigatorTag.setPageContext(pageContext);
	}

	protected void prepareExecution() throws Exception {
		request.setParameter("page", Integer.toString(currentPage));
		pageContext.setAttribute("requestURI", requestURI);

		pagerInfo = new PagerInfo(currentPage, request.getRequestURI(), request.getQueryString(), request.getMethod());
		navigatorTag.setPagerType(type);
		pagerInfo.init(type, totalRows, 10, 10);
		request.setAttribute("pagerInfo", pagerInfo);
		navigatorTag.doStartTag();
	}
}

class MockPageContext extends org.springframework.mock.web.MockPageContext {

	private StringBuilder output = new StringBuilder();

	public MockPageContext(ServletContext context, HttpServletRequest request, HttpServletResponse response) {
		super(context, request, response);
	}

	public String getOutput() {
		return output.toString();
	}

	public JspWriter getOut() {
		return new JspWriter(0, true) {
			@Override
			public void print(String value) throws IOException {
				output.append(value);
			}

			@Override
			public void clear() throws IOException {
			}

			@Override
			public void clearBuffer() throws IOException {
			}

			@Override
			public void close() throws IOException {
			}

			@Override
			public void flush() throws IOException {
			}

			@Override
			public int getRemaining() {
				return 0;
			}

			@Override
			public void newLine() throws IOException {
			}

			@Override
			public void print(boolean value) throws IOException {
				output.append(value);
			}

			@Override
			public void print(char value) throws IOException {
				output.append(value);
			}

			@Override
			public void print(int value) throws IOException {
				output.append(value);
			}

			@Override
			public void print(long value) throws IOException {
				output.append(value);
			}

			@Override
			public void print(float value) throws IOException {
				output.append(value);
			}

			@Override
			public void print(double value) throws IOException {
				output.append(value);
			}

			@Override
			public void print(char[] value) throws IOException {
				output.append(value);
			}

			@Override
			public void print(Object value) throws IOException {
				output.append(value);
			}

			@Override
			public void println() throws IOException {
			}

			@Override
			public void println(boolean value) throws IOException {
				output.append(value);
			}

			@Override
			public void println(char value) throws IOException {
				output.append(value);
			}

			@Override
			public void println(int value) throws IOException {
				output.append(value);
			}

			@Override
			public void println(long value) throws IOException {
				output.append(value);
			}

			@Override
			public void println(float value) throws IOException {
				output.append(value);
			}

			@Override
			public void println(double value) throws IOException {
				output.append(value);
			}

			@Override
			public void println(char[] value) throws IOException {
				output.append(value);
			}

			@Override
			public void println(String value) throws IOException {
				output.append(value);
			}

			@Override
			public void println(Object value) throws IOException {
				output.append(value);
			}

			@Override
			public void write(char[] cbuf, int off, int len) throws IOException {
			}
		};
	}
}